package com.dy.controller;

import com.dy.domain.Shangpin;
import com.dy.domain.Tu;
import com.dy.domain.pojo.PageBean;
import com.dy.domain.pojo.Result;
import com.dy.service.ShangpinService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/shangpin")
public class ShangPinController {

    @Autowired
    private ShangpinService shangpinService;


    //分页查询所有 模糊
    @RequestMapping("/findPage")
    public Result findPage(Integer pageNo,Integer pageSize,String ming){

        PageBean<Shangpin> pageBean = shangpinService.findPage(pageNo,pageSize,ming);

        return new Result().ok(pageBean);
    }


    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Shangpin shangpin){

        shangpinService.add(shangpin);

        return new Result().ok(null);
    }

    //回显
    @RequestMapping("/huixian")
    public Result huixian(Integer sid){

        Shangpin shangpin = shangpinService.huixian(sid);

        return new Result().ok(shangpin);
    }

    //修改
    @RequestMapping("/update")
    public Result update(@RequestBody Shangpin shangpin){

        shangpinService.update(shangpin);

        return new Result().ok(null);
    }

    //删除
    @RequestMapping("/del")
    public Result del(Integer sid){

        shangpinService.del(sid);
        return new Result().ok(null);
    }


    @RequestMapping("/daochu")
    public void daochu(HttpServletResponse response) throws IOException {

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sahngpin_list = workbook.createSheet("shangpin_list");

        HSSFRow row = sahngpin_list.createRow(0);

        row.createCell(0).setCellValue("商品编号");
        row.createCell(1).setCellValue("商品名称");
        row.createCell(2).setCellValue("价格");
        row.createCell(3).setCellValue("分类");

        List<Shangpin> list = shangpinService.findall();

        int i = 1;
        for (Shangpin shangpin : list) {
            HSSFRow row1 = sahngpin_list.createRow(i++);
            row1.createCell(0).setCellValue(shangpin.getSid());
            row1.createCell(1).setCellValue(shangpin.getSname());
            row1.createCell(2).setCellValue(shangpin.getPrice());
            row1.createCell(3).setCellValue(shangpin.getLei());
        }

        //处理文件下载
        response.setContentType("application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=shangpin_zhanshi.xlsx");
        //写数据
        workbook.write(response.getOutputStream());
    }

    //导入
    @RequestMapping("/daoru")
    public void daoru(MultipartFile file, HttpServletResponse response) throws IOException {

        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());

        HSSFSheet sheetAt = workbook.getSheetAt(0);

        int i = 0;

        for (Row cells : sheetAt) {

            if (i==0){
                i++;
                continue;
            }

            String sname = cells.getCell(1).getStringCellValue();
            String price = cells.getCell(2).getStringCellValue();
            String lei = cells.getCell(3).getStringCellValue();

            shangpinService.add(new Shangpin(null,sname,price,lei));
        }

        response.sendRedirect("/admin/shangpin_list.html");

    }


    @RequestMapping("/gettu")
    public List<Tu> gettu(){

        return shangpinService.find();
    }


}
